package com.shinowit.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shinowit.dao.mapper.SysResourceMapper;
import com.shinowit.model.DataGridResult;
import com.shinowit.model.Result;
import com.shinowit.model.SysResource;
import com.shinowit.model.SysResourceCriteria;

@Controller
@RequestMapping(value="/system/resource")
public class SysResourceController {

	@Resource
	private SysResourceMapper sys_resource_mapper_dao;

	@RequestMapping(value={"","/"})
	public String defaultPage(){
		return "/resource/list";
	}
	
	
	
	@RequestMapping(value="/create")
	@ResponseBody
	public Map<String,Object> create_new(SysResource data){
		Map<String,Object> result_map=new HashMap<String,Object>();
		if (data.getParentCode()!=null){
			if (data.getParentCode().trim().length()==0){
				data.setParentCode(null);
			}
		}
		int i=sys_resource_mapper_dao.insert(data);
		if (1==i){
			result_map.put("msg", "保存成功!");
			result_map.put("status", true);
		}else{
			result_map.put("msg", "保存失败!");
			result_map.put("status", false);
		}
		return result_map;
	}

	@RequestMapping(value="/edit")
	@ResponseBody
	public Result edit_data(SysResource data){
		Result result=new Result();
		if (data.getParentCode()!=null){
			if (data.getParentCode().trim().length()==0){
				data.setParentCode(null);
			}
		}
		int i=sys_resource_mapper_dao.updateByPrimaryKey(data);
		if (1==i){
			result.setMsg("修改成功！");
			result.setStatus(true);
		}else{
			result.setMsg("修改失败！");
			result.setStatus(false);
		}
		return result;
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Result delete_data(String resCode){
		Result result=new Result();
		int i=sys_resource_mapper_dao.deleteByPrimaryKey(resCode);
		if (1==i){
			result.setMsg("删除成功！");
			result.setStatus(true);
		}else{
			result.setMsg("删除失败！");
			result.setStatus(false);
		}
		return result;
	}	
		
	@RequestMapping(value="/res_tree_list")
	@ResponseBody
	public DataGridResult<SysResource> getAllResourceTree(){
		DataGridResult<SysResource> result=new DataGridResult<SysResource>();
		result.setTotal(sys_resource_mapper_dao.countByExample(new SysResourceCriteria()));
		SysResourceCriteria ex=new SysResourceCriteria();
		ex.createCriteria().andParentCodeIsNull();
		List<SysResource> datas=sys_resource_mapper_dao.selectByExample(ex);
		for (SysResource current_node:datas){
			getChildNode(current_node);
		}
		result.setRows(datas);
		return result;
	}

   private void getChildNode(SysResource parentNode){
		SysResourceCriteria ex=new SysResourceCriteria();
		ex.createCriteria().andParentCodeEqualTo(parentNode.getSysResCode());
		List<SysResource> datas=sys_resource_mapper_dao.selectByExample(ex);
		for (SysResource current_node:datas){
			parentNode.children.add(current_node);
			getChildNode(current_node);
		}	   
   }
	
}
